/**
 * 
 */
package com.mvc.service.MyMenu.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mvc.dao.BaseDaoI;
import com.mvc.model.healthmanager.archive.BloodOxygen;
import com.mvc.model.healthmanager.archive.BloodPressure;
import com.mvc.model.healthmanager.archive.BloodSugar;
import com.mvc.model.healthmanager.archive.Temperature;
import com.mvc.model.healthmanager.archive.UserTemperature;
import com.mvc.model.healthmanager.archive.WeightHeight;
import com.mvc.model.report.ColligateReport;
import com.mvc.service.MyMenu.ReportServiceI;
import com.mvc.utils.PressureHint;
import com.mvc.utils.StringUtil;
import com.mvc.utils.ValidatorUtil;
import com.mvc.utils.WeightInfoHint;

/**
 * @author qingjie
 * 
 */
@Service
public class ReportServiceImpl implements ReportServiceI {
	
	@Autowired
	private BaseDaoI<ColligateReport> cBaseDaoI;

	@Override
	public Map<String, Object> getPressureForZh(List<BloodPressure> bli,int size) {
		Map<String, Object> bMap = new HashMap<String, Object>();
		if (bli.size() > size) {
			boolean b = true;
			int p1 = 0;
			int p2 = 0;
			int p3 = 0;
			for (BloodPressure bp : bli) {
				if (b) {
					bMap.put("ls", bp.getL_sbp());
					bMap.put("ld", bp.getL_dbp());
					bMap.put("rs", bp.getR_sbp());
					bMap.put("rd", bp.getR_dbp());
					bMap.put("state", bp.getPressureState());
					bMap.put("tishi", PressureHint.getXueyaHint(ValidatorUtil.parseDoubleByString(bp.getL_sbp()+""), ValidatorUtil.parseDoubleByString(bp.getL_dbp()+""), "0", "0", ""));
				}
				if (bp.getL_sbp() < 120 && bp.getL_dbp() < 80) {
					p1++;
				} else if (bp.getL_sbp() >= 140 || bp.getL_dbp() >= 90) {
					p3++;
				} else {
					p2++;
				}
				b = false;
			}
			double pl = (p2 / bli.size()) * 100;
			bMap.put("p1", p1);
			bMap.put("p2", p2);
			bMap.put("p3", p3);
			bMap.put("pl", pl + "%");
			bMap.put("pcr", StringUtil.getCr(pl));
		}
		return bMap;
	}

	//血糖信息
	@Override
	public Map<String, Object> getSugarForZh(List<BloodSugar> sli,int size) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(sli.size()>0){
			boolean b = true;
			int p1 = 0;
			int p2 = 0;
			int p3 = 0;
			if(sli.size()>size){
				BloodSugar bs = sli.get(size);
				map.put("fbg", ValidatorUtil.treatNullOnDouble(bs.getFbg()));
				map.put("fbg_", (int)(ValidatorUtil.treatNullOnDouble(bs.getFbg())*10));
				map.put("ogtt", ValidatorUtil.treatNullOnDouble((bs.getOgtt())));
				map.put("ogtt_", (int)(ValidatorUtil.treatNullOnDouble(bs.getOgtt())*10));
				map.put("state", bs.getSugarState());
				map.put("tc", ValidatorUtil.treatNullOnDouble((bs.getTc())));
				map.put("tg", ValidatorUtil.treatNullOnDouble((bs.getTg())));
				map.put("ua", ValidatorUtil.treatNullOnDouble((bs.getUa())));
				map.put("chol", ValidatorUtil.treatNullOnDouble((bs.getChol())));
				map.put("hdlChol", ValidatorUtil.treatNullOnDouble((bs.getHdlChol())));
				map.put("calChol", ValidatorUtil.treatNullOnDouble((bs.getCalChol())));
				if(bs.getFbg()<3.6){
					p1++;
				}else if(bs.getFbg()<6.1){
					p2++;
				}else p3++;
			}
			
			double sl = p2/(sli.size())*100;
			map.put("p1", p1);
			map.put("p2", p2);
			map.put("p3", p3);
			map.put("sl", sl + "%");
			map.put("scr", StringUtil.getCr(sl));
				
		}
		return map;
	}

	@Override
	public Map<String, Object> getOxyenForZh(List<BloodOxygen> oli) {
		Map<String, Object> map = new HashMap<String, Object>();
		int i = 0;
		if(oli.size()>0){
			double ml[] = new double[oli.size()];
			double o2[] = new double[oli.size()];
			boolean b = true;
			for(BloodOxygen bo : oli){
				if(b){
					map.put("pulse", bo.getPulse());
					map.put("spo2", bo.getSpo2());
				}
				b = false;
				ml[i] = bo.getPulse();
				o2[i] = bo.getSpo2();
				i++;
			}
			double max_ml = 0;
			double min_ml = 0;
			double max_o2 = 0;
			double min_o2 = 0;
			for(i=0;i<oli.size();i++){
				for(int j = i+1; j<oli.size();j++){
					double temp = 0;
					if(ml[i] < ml[j]){
						temp = ml[i];
						ml[i] = ml[j];
						ml[j] = temp;
					}
					if(o2[i] < o2[j]){
						temp = o2[i];
						o2[i] = o2[j];
						o2[j] = temp;
					}
				}
			}
			max_ml = ml[oli.size()-1];min_ml = ml[0];
			max_o2 = o2[oli.size()-1];min_o2 = o2[0];
			map.put("max_ml", max_ml);map.put("min_ml", min_ml);
			map.put("max_o2", max_o2);map.put("min_o2", min_o2);
			map.put("avg_ml", (max_ml+min_ml)/2);
			map.put("avg_o2", (max_o2+min_o2)/2);
		}
		return map;
	}

	//身高体重
	@Override
	public Map<String, Object> getWeightForZh(List<WeightHeight> whli,String gender,int size) {
		Map<String, Object> map = new HashMap<String, Object>();
		int result = 0;int bike = 0;int car = 0;int sport=0;int walk=0;int recreation=0;
		if(whli.size()<=0){
			return null;
		}
		if(whli.size()>0){
			boolean b = true;
			for(WeightHeight wh : whli){
				if(b){
					map.put("height", wh.getHeight());
					map.put("weight", wh.getWeight());
					if(ValidatorUtil.isNullOnDouble(wh.getBmi())){
						double bmi = wh.getWeight() / ((wh.getHeight()*wh.getHeight())/10000);
						wh.setBmi((double)Math.round(bmi*100)/100);
					}
					map.put("bmi", wh.getBmi());
					map.put("waist", wh.getWaist());
					map.put("hipline", wh.getHipline());
					map.put("tishi", WeightInfoHint.getTiZhongHint(gender, wh.getBmi(), wh.getWaist()));
					map.put("whi", (wh.getWaist()/ValidatorUtil.treatNullOnDouble(wh.getHipline()))*100);
					//家务
					if(wh.getHouseWork()!=null){
						result = 0;
						String[] str = wh.getHouseWork().split(";");
						for(String s :str){
							String[] ss = s.split(",");
							if(ss.length==4){
								result += Integer.parseInt(ValidatorUtil.treatNotNumber(ss[1])) * Integer.parseInt(ValidatorUtil.treatNotNumber(ss[2]));
							}
						}
					}
					//步行
					if(wh.getInstrument()!=null){
						String[] str = wh.getHouseWork().split(";");
						for(String s :str){
							String[] ss = s.split(",");
							if(ss.length==4){
								if(ss[0].equals("walk-stay")||ss[0].equals("walk-50-55")||ss[0].equals("walk-110-120")||ss[0].equals("walk-120")){
									walk += Integer.parseInt(ValidatorUtil.treatNotNumber(ss[1])) * Integer.parseInt(ValidatorUtil.treatNotNumber(ss[2]));
								}else if(ss[0].equals("riding")){
									car += Integer.parseInt(ValidatorUtil.treatNotNumber(ss[1])) * Integer.parseInt(ValidatorUtil.treatNotNumber(ss[2]));
								}
							}
						}
					}
					//锻炼
					if(wh.getSports()!=null){
						String[] str = wh.getSports().split(";");
						for(String s :str){
							String[] ss = s.split(",");
							if(ss.length==4){
								if(ss[0].equals("riding-bicycle-slow")||ss[0].equals("riding-bicycle-quickly")){
									bike += Integer.parseInt(ValidatorUtil.treatNotNumber(ss[1])) * Integer.parseInt(ValidatorUtil.treatNotNumber(ss[2]));
								}else sport += Integer.parseInt(ValidatorUtil.treatNotNumber(ss[1])) * Integer.parseInt(ValidatorUtil.treatNotNumber(ss[2]));
							}
						}
					}
					//闲暇
					if(wh.getRecreation()!=null){
						String[] str = wh.getSports().split(";");
						for(String s :str){
							String[] ss = s.split(",");
							if(ss.length==4){
								recreation += Integer.parseInt(ValidatorUtil.treatNotNumber(ss[1])) * Integer.parseInt(ValidatorUtil.treatNotNumber(ss[2]));
							}
						}
					}
					map.put("houseWork", result);//家务
					map.put("car", car);//乘车开车
					map.put("walk", walk);//步行
					map.put("bike", bike);//骑自行车
					map.put("sport", sport);//锻炼
					map.put("recreation", recreation);//闲暇
					map.put("work", 0);//静态工作
					map.put("strongWork", 0);//中等以上工作
					map.put("sleep", 0);//睡眠
					b = false;
				}else break;
			}
		}
		return map;
	}

	@Override
	public Map<String, Object> getTemperatureForZh(List<UserTemperature> tli) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(tli.size()>0){
			for(UserTemperature ut : tli){
				map.put("temperature", ut.getTemperature());
			}
		}
		return map;
	}
	
	/**
	 * 
	 * @param val
	 * @return
	 */
	public int getSportTime(String val){
		int result = 0;
		String[] str = val.split(";");
		for(String s :str){
			String[] s1 = s.split(",");
			if(s1.length==4){
				result += Integer.parseInt(ValidatorUtil.treatNotNumber(s1[1])) * Integer.parseInt(ValidatorUtil.treatNotNumber(s1[2]));
			}
		}
		return result;
	}

	@Override
	public List<ColligateReport> findColligateReport(Map<String, Object> params) {
		return cBaseDaoI.find(StringUtil.getHql("ColligateReport", params, "desc"),params);
	}

	@Override
	public ColligateReport getColligateReportById(String id) {
		return cBaseDaoI.get("from ColligateReport c where c.id = '"+Long.parseLong(id)+"'");
	}


}
